(function() {
  var leftMenuList = [
    {
      type: 'menu',
      name: 'studentpersonalcenter',
      src: 'student_personal_center.html',
      text: i18next.t('index.personinfo')//'个人信息'
    },
    {
      type: 'menu',
      name: 'Studentmanagement',
      src: 'student_order.html',
      text: i18next.t('index.myorder') //'我的订单'
    },
    {
      type: 'menu',
      name: 'teacherInfo',
      src: 'student_course.html',
      text: i18next.t('index.mycourse')//'我的课程'
    },

    {
      type: 'menu',
      name: 'studentappintupdate',
      src: 'student_appintupdate.html',
      text: i18next.t('index.myappointment')//'我的约课'
    },
    {
      type: 'menu',
      name: 'studentlessontime',
      src: 'student_lessontimes.html',
      text: i18next.t('index.mycoursetable')//'我的课表'
    },
    {
      type: 'menu',
      name: 'job_title',
      src: 'job_title.html',
      text: i18next.t('index.myhomework')//'我的作业'
    },
    {
      type: 'menu',
      name: 'feedback',
      src: 'feedback.html',
      text: i18next.t('index.classroomfeedback')//'课堂反馈'
    },
    {
      type: 'menu',
      name: 'student_remind_set',
      src: 'student_remind_set.html',
      text: i18next.t('index.notifyset')//'提醒设置'
    }

  ]

  function leftMenuInit(leftMenuList, curHtml) {
    var $listGroup = $('<ul class="menu-list"></ul>');
    $.each(leftMenuList, function (i, item) {
      if(item.type.indexOf('menu') > -1) {
        var $item = $('<li class="menu"><span class="text">'+ item.text +'</span></li>').appendTo($listGroup)
        if(i == 0) {
          $item.addClass('active')
        }
        if(item.type.indexOf('has-sub') > -1) {
          $item.addClass('has-sub');
        }
        if(item.type.indexOf('submenu') > -1) {
          $item.addClass('submenu');
        }

        if(item.src) {
          $item.on('click', function () {
            $(this).addClass('active').siblings().removeClass('active');
            // $('iframe').attr('src', item.src)
            locationHash(item.src)
          })
        }

        if(curHtml && item.src == curHtml) {
          //$item.addClass('active').siblings().removeClass('active')
          $item.addClass('active').siblings().removeClass('active')
        }

      } else {
        $('<li class="title"><span class="text">'+ item.text +'</span></li>').appendTo($listGroup)
      }
    })
    $('.left-content').append($listGroup)
  }

  function locationHash(hashStr) {
    if(hashStr) {
      location.hash = hashStr.split('.html')[0]
    }
    if(location.hash) {
      $('iframe').attr('src', location.hash.substr(1) + '.html')
    } else {
      $('iframe').attr('src', 'student_personal_center.html')
    }
  }

  locationHash()
  leftMenuInit(leftMenuList, location.hash.substr(1) + '.html');

  $('#J_logout').on('click', function () {
    ajaxMethod({
      url: '/api/user/logout',
      method: 'post',
      success: function (data, status) {
        if(status.code == 0) {
          location.href = '/'
        }
      }
    })
  })

  function getlogo(){
    $.ajax({
      type:'get',
      url:'/api/organization/1',
      success:function (logo) {
        if(logo.data.agencyreferred)
          $('.logo').text(logo.data.agencyreferred);
        else {
          if (logo.data.orgname)
            $('.logo').text(logo.data.orgname);
        }
      }
    });
  }
  getlogo();

  function  getCurrentUser() {
    $.ajax({
      type:'get',
      url:'/api/user/current',
      success:function (user) {
        $('.login-user').text('当前用户： '+(user.data.account || user.data.number));
        $('#J_logout').show()
      }
    });
  }
  getCurrentUser();
}());
